package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.bean.query.QueryOutStorageInventoryAddressBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryOutStorageInventoryAddressRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.QueryUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.List;

public class QueryOutStorageSelectActivity extends BaseActivity {

    private TextView title, inventoryAddress, nextStep, dateBegin, dateEnd;

    private TextView inventoryAddressValue;

    private EditText outStorageNumber;

    private List<QueryOutStorageInventoryAddressBean.DataBean.InventoryAddressListBean> mInventoryAddressList;

    private static final long ONE_MONTH = 1000L * 60L * 60L * 24L * 30L;

    @Override

    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_select);
        initView(QueryOutStorageSelectActivity.this);
        initData(QueryOutStorageSelectActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        inventoryAddress = (TextView) activity.findViewById(R.id.inventoryAddress);
        inventoryAddressValue = (TextView) activity.findViewById(R.id.inventoryAddressValue);
        dateBegin = (TextView) activity.findViewById(R.id.dateBegin);
        dateEnd = (TextView) activity.findViewById(R.id.dateEnd);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        outStorageNumber = (EditText) activity.findViewById(R.id.outStorageNumber);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_query_manager_outbound_query));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_free_list_inquiry), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            outStorageNumber.setText(QRCodeUtils.DecryptQRCode(scanResult));
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void getData(Context context) {

        QueryOutStorageInventoryAddressRequest.request(context, new GsonBeanListener<QueryOutStorageInventoryAddressBean>() {
            @Override
            public void onSuccess(QueryOutStorageInventoryAddressBean bean) {
                mInventoryAddressList = bean.getData().getInventoryAddressList();
                initSpinner(context);
            }
        });
        dateBegin.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, Utils.getBeginOfMonth()));
        dateBegin.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                QueryUtils.getTimePickerDialog(context, Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, dateBegin.getText().toString()), System.currentTimeMillis() - ONE_MONTH, System.currentTimeMillis(), new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        dateBegin.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                    }
                }).show(getSupportFragmentManager(), "datePickerBegin");
            }
        });

        dateEnd.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, Utils.getBeginOfMonth() + ONE_MONTH));
        dateEnd.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                QueryUtils.getTimePickerDialog(context, Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, dateEnd.getText().toString()), System.currentTimeMillis(), System.currentTimeMillis() + ONE_MONTH, new OnDateSetListener() {
                    @Override
                    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                        dateEnd.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                    }
                }).show(getSupportFragmentManager(), "datePickerBegin");
            }
        });

        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String checkResult = check(context);
                if (checkResult == null) {
                    Intent intent = new Intent(context, QueryOutStorageListActivity.class);
                    intent.putExtra(QueryExtra.INVENTORY_ADDRESS_CODE, inventoryAddressValue.getText().toString());
                    intent.putExtra(QueryExtra.OUT_STORAGE_NUMBER, outStorageNumber.getText().toString());
                    intent.putExtra(QueryExtra.DATE_BEGIN, dateBegin.getText().toString());
                    intent.putExtra(QueryExtra.DATE_END, dateEnd.getText().toString());
                    startActivity(intent);
                } else {
                    Utils.middleToast(context, checkResult);
                }

            }
        });
    }

    private String check(Context context) {
        if (TextUtils.isEmpty(inventoryAddressValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint);
        }
        return null;
    }

    private void initSpinner(Context context) {
        inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getQueryOutStorageDialog(context, mInventoryAddressList, new DialogUtils.QueryOutStorageListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value) {
                        if (!text.equals(inventoryAddress.getText().toString())) {
                            inventoryAddress.setText(text);
                            inventoryAddressValue.setText(value);
                        }
                    }
                }).show();
            }
        });
    }

}
